package com.ruoyi.client.utils;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

import java.io.File;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class Minio {
    public static void upload() {
        try {
            // 创建 MinioClient 实例
            MinioClient minioClient =
                MinioClient.builder()
                    .endpoint("http://47.109.67.79:9000") // 修正 endpoint
                    .credentials("root", "root284210")
                    .build();

            // 检查桶是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket("test").build());
            if (!found) {
                // 创建新桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket("test").build());
                System.out.println("Bucket 'test' created successfully.");
            } else {
                System.out.println("Bucket 'test' already exists.");
            }

            // 上传文件
            minioClient.uploadObject(
                UploadObjectArgs.builder()
                    .bucket("test")
                    .object("folder/img.png")
                    .filename(new File("D:/图片/4k/desk-18号_LE_auto_x2_light_ai.jpg").getAbsolutePath()) // 修正文件路径
                    .build());

            System.out.println(
                "'D:/图片/4k/desk-18号_LE_auto_x2_light_ai.jpg' is successfully uploaded as "
                    + "object 'img.png' to bucket 'test'.");
        } catch (MinioException | IOException e) {
            System.out.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        } catch (NoSuchAlgorithmException | InvalidKeyException e) {
            System.out.println("Error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
